package fr.cpe.j2ee.projet.war;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import fr.cpe.j2ee.projet.ejb.ActivitySession;
import fr.cpe.j2ee.projet.ejb.EmployeeSession;
import fr.cpe.j2ee.projet.model.Activity;
import fr.cpe.j2ee.projet.model.Employee;
import fr.cpe.j2ee.projet.model.Project;

public class LoginBean 
{
	private String name;
    private String password;
    private Employee employee;
    
    @EJB
    private EmployeeSession employeeSession;
    
    @EJB
    private ActivitySession activitySession;
    
    public LoginBean() {
		employee = null;
	}
    
    /**
     * Méthode gérant la connexion au site, et la redirection vers les pages correspondantes
     */
    public String logUser()
    {
		Employee emp = employeeSession.login(name, password);
		if (emp != null) {
			System.out.println("> User");
			employee = emp;
			
			// On retourne "user", pour que la "navigation-rule" redirige vers la page user
			if(isAdmin()) {
				return "admin";
			} else {
				return "user";
			}
		} else {
			System.out.println("> Erreur");
			// Message d'erreur lorsque la connexion a échoué
			FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR,"Erreur d'identification","Veuillez vérifier vos identifiants et tenter à nouveau de vous connecter"));
			return "nonsuccess";
    		
		}
    }
    
    /**
     *  Déconnexion de l'utilisateur
     * @return String
     */
    public String logOut() 
    {
    	System.out.println("Déconnexion de l'utilisateur");
    	FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    	// Navigation rule pour rediriger vers la page d'accueil
    	return "home";
    }
    
    /**
     * Savoir si un utilisateur est connecté ou non
     * @return
     */
    public boolean isLogged() {
    	return employee != null;
    }
    
    /**
     * 
     * @return true si l'utilisateur est l'admin
     */
    public boolean isAdmin() {
    	if(employee != null) {
    		return getEmployee().getEmpUsername().equals("admin");
    	}
    	return false; 
    }

    /**
     * Getters and setters
     */
    public String getName ()
    {
        return name;
    }
    public void setName (final String name)
    {
        this.name = name;
    }
    public String getPassword ()
    {
        return password;
    }
    public void setPassword (final String password)
    {
        this.password = password;
    }
    public Employee getEmployee() {
		return employee;
	}
    
    public List<Project> getProjects()
    {
    	List<Project> projects = new ArrayList<Project>();
    	
    	if(this.employee != null)
    	{
    		Set<Activity> activities = this.employee.getActivities();
	    	
	    	for(Activity activ : activities)
	    	{
	    		Project proj = activ.getProject();
	    		
	    		if(this.employee.equals(activ.getEmployee()) && !projects.contains(proj))
	    		{
	    			projects.add(proj);
	    		}
	    	}
    	}
    	
    	return projects;
    }
    
//    public HashMap<Project, List<Employee>> getCoWorkers()
    public List<Map.Entry<Project, List<Employee>>> getCoWorkers()
    {
    	HashMap<Project, List<Employee>> result = new HashMap<Project, List<Employee>>(3);
    	List<Project> projects = this.getProjects();
    	
    	for (Project p : projects)
    	{
    		List<Employee> cow = new ArrayList<Employee>(5);
    		List<Employee> emps = this.activitySession.getWorkers(p);
    		for (Employee e : emps)
    		{
    			if (!cow.contains(e) && e.getEmpId() != this.employee.getEmpId())
    			{
    				cow.add(e);
    			}
    		}
    		
    		result.put(p, cow);
    	}
    	
    	return new ArrayList<Map.Entry<Project, List<Employee>>>(result.entrySet());
    }
}
